Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI.WebControls
Imports InputControls
Imports CDM.DataCore
Imports CDM.DataCore.Entities
Imports Cdm.DataCore.UserSecurity
Imports System.Collections
Imports System.Configuration
Imports System.Web
Imports System.Web.UI


Public Class EntityController
    ''' <summary>
    ''' Loops through all the page controls, and sets the values of all those fields by matching
    ''' the property of the ent object to the BoundProperty of the input control  Also determines
    ''' whether each field should be marked as required, and whether they are editable.
    ''' </summary>
    ''' <param name="permContainer">The Page or UserControl that Implements IPermission</param>
    
    Public Shared Sub SetPagePropertiesFromObject(ByVal permContainer As IPermission, ByVal setValues As Boolean)
        SetPagePropertiesFromObject(permContainer, setValues, permContainer.PrimaryObject.EntityTableName)
    End Sub

    Public Shared Sub SetPagePropertiesFromObject(ByVal permContainer As IPermission, ByVal setValues As Boolean, _
                    ByVal entityTableName As String)

        Dim ent As ConfigurableObject = permContainer.PrimaryObject
        Dim isEditable As Boolean = UserAuthorization.HasUpdatePermission(permContainer.EntityPermissionType)
        For Each ctrl As Control In permContainer.ControlCollection
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                iinput.Required = isRequiredField(ent.RequiredFields, iinput.BoundProperty)
                If setValues Then
                    iinput.Value = ent.GetPropertyValue(iinput.BoundProperty)
                    iinput.Title = ReplaceDisplayName(iinput.Title)
                End If
                iinput.Editable = UserHasEditFieldPermission(iinput.BoundProperty, entityTableName) And isEditable
                iinput.cssClassControl = GetcssClassControl(iinput)

            ElseIf TypeOf (ctrl) Is Label And setValues = True Then
                Dim lbl As Label = ctrl
                lbl.Text = ReplaceDisplayName(lbl.Text)
            End If

            If IsParent(ctrl) Then
                SetPagePropertiesFromObject(ctrl.Controls, ent, isEditable, setValues, entityTableName)
            End If

        Next

    End Sub

    ''' <summary>
    ''' Returns the current participant
    ''' For Participant users, legalguardian users and participant rep users, retrieve
    ''' that data from the current user information
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetParticipant() As Cdm.DataCore.Entities.Participant

        If IsSingleParticipantUser() then
            Return UserAuthorization.CurrentUser.Participant
        Else
            Return System.Web.HttpContext.Current.Session("Participant")
        End If

    End Function
    ''' <summary>
    ''' Get the inactivity period from the application settings.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetInactivePeriod() As Integer
        If System.Web.HttpContext.Current.Application("inactiveperiod") Is Nothing Then
            Dim configSettings As Cdm.DataCore.LookupTables.LuPasswordConfiguration
            configSettings = Cdm.DataCore.LookupTables.LuPasswordConfiguration.ListAll().Item(0)
            System.Web.HttpContext.Current.Application("inactiveperiod") = configSettings.InactivityPeriod
        End If

        Return Convert.ToInt32(System.Web.HttpContext.Current.Application("inactiveperiod"))
    End Function

    ''' <summary>
    ''' A one-to one match with the database value
    ''' </summary>
    ''' <param name="databaseValue">The value in the database</param>
    ''' <returns>the matching display name.  If not found, the input databaseValue is returned.</returns>
    ''' <remarks></remarks>
    Public Shared Function GetSingleDisplayName(ByVal databaseValue As String) As String
        SetApplicationDisplayNames()
        Dim hsh As Hashtable = System.Web.HttpContext.Current.Application("displayNames")
        If hsh.Item(databaseValue) Is Nothing Then
            Return databaseValue
        Else
            Return hsh.Item(databaseValue)
        End If
    End Function

    ''' <summary>
    ''' Loops through all the displayname variables and Replaces the database value string
    ''' with the 
    ''' </summary>
    ''' <param name="inputString">The string to replace with all the display settings</param>
    ''' <returns>A string with all the database values replaced with display values</returns>
    ''' <remarks></remarks>
    Public Shared Function ReplaceDisplayName(ByVal inputString) As String
        Dim lst As System.Collections.Generic.List(Of Cdm.DataCore.Admin.DisplayNames) = Cdm.DataCore.Admin.DisplayNames.ListAll()
        Dim i As Integer
        Dim returnString As String = inputString
        For i = 0 To lst.Count - 1
            returnString = returnString.Replace(lst(i).DBName, lst(i).DisplayName)
        Next
        Return returnString
    End Function

    Public Overloads Shared Sub SetApplicationDisplayNames()
        If System.Web.HttpContext.Current.Application("displayNames") Is Nothing Then
            System.Web.HttpContext.Current.Application("displayNames") = Cdm.DataCore.Admin.DisplayNames.RetrieveHashTable()
        End If
    End Sub

    Public Overloads Shared Sub SetApplicationDisplayNames(ByVal forceNewQuery As Boolean)
        If forceNewQuery = True Then
            System.Web.HttpContext.Current.Application("displayNames") = Cdm.DataCore.Admin.DisplayNames.RetrieveHashTable()
        Else
            SetApplicationDisplayNames()
        End If

    End Sub


    Public Shared Sub SetParticipant(ByVal newParticipant As Participant)
        System.Web.HttpContext.Current.Session("Participant") = newParticipant
    End Sub

    Private Shared Function GetcssClassControl(ByVal iinput As IInputControl) As String
        If iinput.Editable = False Then
            Return "text"
        Else
            Select Case iinput.ControlType.ToUpper()
                Case "TEXT", "CHECKBOX", "CHECKBOXLIST"
                    Return "form"
                Case "DATE"
                    Return "formDate"
                Case "RADIO"
                    Return "formRadio"
                Case "DROPDOWN"
                    Return "formSelect"
                Case "SSN", "PHONE"
                    Return "formSelectMonth"
                Case Else
                    Return "form"
            End Select
        End If

    End Function
    ''' <summary>
    ''' Loops through the controls collection, and sets the values of all those fields by matching
    ''' the property of the ent object to the BoundProperty of the input control.  Also determines
    ''' whether each field should be marked as required, and whether they are editable.
    ''' </summary>
    ''' <param name="cll">The control collection that we loop through.</param>
    ''' <param name="ent"></param>
    ''' <remarks></remarks>
    Public Shared Sub SetPagePropertiesFromObject(ByVal cll As ControlCollection, ByVal ent As Cdm.DataCore.ConfigurableObject, _
                 ByVal isEditable As Boolean, ByVal setValues As Boolean, ByVal entityTableName As String)
        For Each ctrl As Control In cll
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                iinput.Title = ReplaceDisplayName(iinput.Title)
                iinput.Required = isRequiredField(ent.RequiredFields, iinput.BoundProperty)
                If setValues = True Then
                    iinput.Value = ent.GetPropertyValue(iinput.BoundProperty)
                End If

                iinput.Editable = UserHasEditFieldPermission(iinput.BoundProperty, entityTableName) And isEditable
                iinput.cssClassControl = GetcssClassControl(iinput)

            ElseIf TypeOf (ctrl) Is Label Then
                Dim lbl As Label = ctrl
                lbl.Text = ReplaceDisplayName(lbl.Text)
            End If

            If IsParent(ctrl) Then
                SetPagePropertiesFromObject(ctrl.Controls, ent, isEditable, setValues, entityTableName)
            End If

        Next
    End Sub

    Public Shared Sub SetPageDisplayNames(ByVal cll As ControlCollection)
        For Each ctrl As Control In cll
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                iinput.Title = ReplaceDisplayName(iinput.Title)
            End If
            If IsParent(ctrl) Then
                SetPageDisplayNames(ctrl.Controls)
            End If
        Next
    End Sub

    ''' <summary>
    ''' Sets the values without trying to re
    ''' </summary>
    ''' <param name="cll"></param>
    ''' <param name="ent"></param>
    ''' <param name="isEditable"></param>
    ''' <remarks></remarks>
    Public Shared Sub SetPageValuesFromObject(ByVal cll As ControlCollection, ByVal ent As Cdm.DataCore.ConfigurableObject, ByVal isEditable As Boolean)
        For Each ctrl As Control In cll
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                iinput.Value = ent.GetPropertyValue(iinput.BoundProperty)

            End If

            If IsParent(ctrl) Then
                SetPageValuesFromObject(ctrl.Controls, ent, isEditable)
            End If

        Next
    End Sub
    ''' <summary>
    ''' Checks is a control has child controls
    ''' Exceptions - return false for a user control and a specialfield control
    ''' Those objects should be handled internally and not from a parent object.
    ''' </summary>
    ''' <param name="ctrl">The control object to check for subs</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function IsParent(ByVal ctrl As System.Web.UI.Control) As Boolean

        If TypeOf (ctrl) Is MasterPage Then
            Return ctrl.HasControls
        ElseIf IsInputControl(ctrl) Then
            Return False
        ElseIf TypeOf (ctrl) Is System.Web.UI.UserControl Then
            Return False
        ElseIf ctrl.GetType.Name.ToLower.Contains("specialfieldcontrol") Then
            Return False
        Else
            Return ctrl.HasControls
        End If



    End Function

    Public Shared Sub JavaScriptAddChildDiv(ByVal elementId As String, ByVal elementSizeDifference As Integer)

        If HttpContext.Current.Session("ChildDivs") Is Nothing Then
            HttpContext.Current.Session("ChildDivs") = elementId
            HttpContext.Current.Session("ChildDivSpacing") = elementSizeDifference
        Else
            HttpContext.Current.Session("ChildDivs") = String.Concat(HttpContext.Current.Session("ChildDivs").ToString(), ",", elementId)
            HttpContext.Current.Session("ChildDivSpacing") = String.Concat(HttpContext.Current.Session("ChildDivSpacing").ToString(), ",", elementSizeDifference)
        End If

        'Dim jsConst As String = "<script language='javascript'>AddChildElement('{0}', {1});</script>"
        'Dim javaString As String = String.Format(jsConst, elementId, elementSizeDifference.ToString())
        'Return javaString
    End Sub

    Public Shared Function JavaScriptScrollingGridStartup(ByVal scrGrid As ScrollingGridView) As String
        Dim scrollDivJavaScript As String
        'scrollDivJavaScript = String.Format("<script type=""text/javascript"" language=""javascript""> setScrollFromHidden('{0}','{1}'); </script>", _
        '                     scrGrid.DivClientId, scrGrid.HiddenId)

        scrollDivJavaScript = String.Format("setScrollFromHidden('{0}','{1}'); SetPageDivs();", scrGrid.DivClientId, scrGrid.HiddenId)
        Return scrollDivJavaScript

    End Function

    Public Shared Function JavaScriptScrollingDivStartup(ByVal divId As String) As String

    End Function
    Public Shared Sub JavaScriptSetFixedHeight(ByVal fixedDivHeight As Integer)
        HttpContext.Current.Session("FixedDivHeight") = fixedDivHeight
    End Sub

    Public Shared Sub SetAdminStatus(ByVal current As System.Web.UI.Page, ByVal message As String)

    End Sub

    Public Shared Sub SetObjectPropertiesFromPage(ByVal permContainer As IPermission)
        Dim ent As ConfigurableObject = permContainer.PrimaryObject

        For Each ctrl As Control In permContainer.ControlCollection
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                If iinput.Editable Then
                    ent.SetProperty(iinput.BoundProperty, iinput.Value, iinput.Title, permContainer.SectionName)
                Else
                    ent.SetSectionName(iinput.Title, permContainer.SectionName)
                    ent.SetDisplayName(iinput.BoundProperty, iinput.Title)
                End If
            End If

            If IsParent(ctrl) Then
                If ctrl.HasControls Then
                    SetObjectPropertiesFromPage(ctrl.Controls, ent, permContainer.SectionName)
                End If
            End If
        Next

    End Sub


    Public Shared Sub SetObjectPropertiesFromPage(ByVal cll As ControlCollection, ByVal ent As Cdm.DataCore.ConfigurableObject, ByVal sectionName As String)

        For Each ctrl As Control In cll
            If IsInputControl(ctrl) Then
                Dim iinput As IInputControl = CType(ctrl, IInputControl)
                If iinput.Editable Then
                    ent.SetProperty(iinput.BoundProperty, iinput.Value, iinput.Title, sectionName)
                Else
                    ent.SetSectionName(iinput.Title, sectionName)
                    ent.SetDisplayName(iinput.BoundProperty, iinput.Title)
                End If
            End If


            If IsParent(ctrl) Then
                If ctrl.HasControls Then
                    SetObjectPropertiesFromPage(ctrl.Controls, ent, sectionName)
                End If
            End If
        Next

    End Sub

    Public Shared Function IsInputControl(ByVal ctrl As Control) As Boolean
        'Dim inpt As IInputControl
        Try
            If TypeOf (ctrl) Is IInputControl Then
                Return True
            Else
                Return False
            End If
            'inpt = CType(ctrl, IInputControl)
            'Return True
        Catch
            Return False
        End Try
    End Function

    Public Shared Function IsMenuControl(ByVal ctrl As Control) As Boolean
        Try
            If TypeOf (ctrl) Is IMenu Then Return True
            If TypeOf (ctrl) Is InputControls.EditCaseSubNav Then Return True
            If TypeOf (ctrl) Is InputControls35.EditCaseSubNav Then Return True
            If TypeOf (ctrl) Is InputControls.FinancialSubNav Then Return True
            If TypeOf (ctrl) Is InputControls35.FinancialSubNav Then Return True

            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function


    ''' <summary>
    ''' Returns true if fieldName is in the list of Required fields
    ''' <param name="RequiredFields">The list of required fields to check</param>
    ''' <param name="fieldname">the field name to look for</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' </summary>
    Private Shared Function isRequiredField(ByVal RequiredFields As ArrayList, ByVal fieldname As String) As Boolean
        If RequiredFields Is Nothing Then
            Return False
        End If

        If (RequiredFields.IndexOf(fieldname.ToUpper()) > -1) Then
            Return True
        Else
            Return False
        End If

    End Function
    ''' <summary>
    ''' Checks if the user has Edit Field permission for the table and property passed in
    ''' </summary>
    ''' <param name="propertyName">The name of the Property</param>
    ''' <param name="entityTableName">The Entity (Participant, ParticipantEmployee, SupportBroker, etc) that has the property</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function UserHasEditFieldPermission(ByVal propertyName As String, ByVal entityTableName As String) As Boolean
        Return UserAuthorization.CurrentUser.HasFormLevelPermission(entityTableName, propertyName)
    End Function

    Public Shared Sub SetSubNavTabs(ByVal editCaseSubNav1 As InputControls.EditCaseSubNav)
        editCaseSubNav1.DisplayNameList = System.Web.HttpContext.Current.Application("displayNames")
        editCaseSubNav1.IsSmallToolbar = Not (UserAuthorization.CurrentUser.IsSingleParticipantUser())
        editCaseSubNav1.ShowAffirmations = False
        editCaseSubNav1.ShowCaseNotes = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseNotes)
        editCaseSubNav1.ShowEmployeeInfo = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseEmployee)
        If IsSingleParticipantUser() Then
            editCaseSubNav1.ShowFinancial = False
        Else
            editCaseSubNav1.ShowFinancial = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseFinancial)
        End If
        editCaseSubNav1.ShowFMSA = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseFMSA)
        editCaseSubNav1.ShowLegalGuardianInfo = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseLegalGuardian)
        editCaseSubNav1.ShowMedicalCareProvider = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseMedicalProvider)
        editCaseSubNav1.ShowParticipantRepInfo = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseParticipantRep)
        editCaseSubNav1.ShowPersonalInfo = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CasePersonal)
        editCaseSubNav1.ShowProgram = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseProgramInfo)
        editCaseSubNav1.ShowSupportBroker = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseSupportBroker)
        editCaseSubNav1.ShowUserInfo = UserAuthorization.HasViewPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseUserInfo)
        'NPO has requested that user info be removed.  So be it.
        editCaseSubNav1.ShowUserInfo = False
        editCaseSubNav1.ShowDocuments = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseDocuments)
        If ConfigurationManager.AppSettings("AssessmentsUrl") Is Nothing Then
            editCaseSubNav1.ShowAssessments = False
        Else
            editCaseSubNav1.ShowAssessments = UserAuthorization.HasViewPermission(Permission.PermissionType.Assessments)
        End If


    End Sub

    Public Shared Sub ReturnToCasePersonalPage(ByVal editCaseSubNav1 As InputControls.EditCaseSubNav)
        Dim newPage As String = String.Format("~/case/casePersonal.aspx?ActiveTab={0}", editCaseSubNav1.SelectedButton)
        System.Web.HttpContext.Current.Response.Redirect(newPage)
    End Sub


    Public Shared Sub SetSubNavTabsFinancial(ByVal editCaseSubNav1 As InputControls.EditCaseSubNav)
        If IsSingleParticipantUser() Then
            editCaseSubNav1.Visible = False
        Else
            SetSubNavTabs(editCaseSubNav1)
            editCaseSubNav1.SelectedButton = InputControls.EditCaseSubNav.BUTTON_FINANCIAL
        End If
    End Sub

    ''' <summary>
    ''' Sets the visibility of the sub-sub tabs for the financial pages
    ''' Tabs like View Monthly Statement, Submit Spending Plan, Affirmations, History
    ''' </summary>
    ''' <param name="financialSubNav"></param>
    ''' <remarks></remarks>
    Public Shared Sub SetFinancialTab(ByVal financialSubNav As InputControls.FinancialSubNav, _
                                        ByVal pageSubNav As InputControls.EditCaseSubNav)
        financialSubNav.IsSmallToolbar = Not (UserAuthorization.CurrentUser.IsSingleParticipantUser())
        financialSubNav.IsSmallFont = pageSubNav.IsSmallFont
        financialSubNav.DisplayNameList = System.Web.HttpContext.Current.Application("displayNames")
        financialSubNav.ShowAffirmations = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseAffirmations)
        financialSubNav.ShowBudgetInfo = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseBudget)
        financialSubNav.ShowGetMonthlyStatement = UserAuthorization.HasUpdatePermission(Permission.PermissionType.CaseMonthlyStatement)
        financialSubNav.ShowGetSpendingPlan = UserAuthorization.HasUpdatePermission(Permission.PermissionType.CaseSpendingPlan)
        financialSubNav.ShowHistory = (UserAuthorization.HasViewPermission(Permission.PermissionType.CaseMonthlyStatement) And _
                                        UserAuthorization.HasViewPermission(Permission.PermissionType.CaseSpendingPlan))
        financialSubNav.ShowSubmitMonthlyStatement = UserAuthorization.HasUpdatePermission(Permission.PermissionType.CaseMonthlyStatement)
        financialSubNav.ShowSubmitSpendingPlan = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseSpendingPlan)
        financialSubNav.ShowViewMonthlyStatement = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseMonthlyStatement)
        financialSubNav.ShowViewSpendingPlan = UserAuthorization.HasViewPermission(Permission.PermissionType.CaseSpendingPlan)
    End Sub

    Public Shared Sub SetSubNavTabsAssessments(ByVal editCaseSubNav1 As InputControls.EditCaseSubNav)
        SetSubNavTabs(editCaseSubNav1)
        editCaseSubNav1.SelectedButton = InputControls.EditCaseSubNav.BUTTON_ASSESSMENTS
    End Sub

    Public Shared Function IsSingleParticipantUser() As Boolean
        Select Case UserAuthorization.CurrentUser.RoleId
            Case Convert.ToInt32(Cdm.DataCore.UserSecurity.Role.RoleType.LegalGuardian), _
                 Convert.ToInt32(Cdm.DataCore.UserSecurity.Role.RoleType.Representative), _
                 Convert.ToInt32(Cdm.DataCore.UserSecurity.Role.RoleType.Participant)
                Return True
            Case Else
                Return False
        End Select
    End Function

    Public Shared Function IsGuestUser() As Boolean
        Select Case CType(UserAuthorization.CurrentUser.RoleId, Cdm.DataCore.UserSecurity.Role.RoleType)
            Case Role.RoleType.Guest, Role.RoleType.Guest2, Role.RoleType.Guest3, Role.RoleType.Guest4
                Return True
            Case Else
                Return False
        End Select
    End Function
    ''' <summary>
    ''' Calls the notification send method, sending the application settings for
    ''' email address and email server
    ''' </summary>
    ''' <param name="notificationTypeValue"></param>
    ''' <param name="participantId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function SendNotification(ByVal notificationTypeValue As Cdm.DataCore.CaseNotes.Notification.NotificationType, ByVal participantId As Integer) As Boolean
        Dim fromEmail As String = System.Web.HttpContext.Current.Application("EmailFromAddress")
        Dim emailServer As String = System.Web.HttpContext.Current.Application("EmailServer")
        Dim emailFromDisplayName As String = System.Web.HttpContext.Current.Application("EmailFromDisplayName")
        Dim apphome As String = ApplicationHomePage()
        Return Cdm.DataCore.CaseNotes.Notification.Send(notificationTypeValue, participantId, fromEmail, emailServer, emailFromDisplayName, apphome)
    End Function
    ''' <summary>
    ''' Calls the notification send method, sending the application settings for
    ''' email address and email server - Overloaded method where list of roles is supplied
    ''' </summary>
    ''' <param name="notificationTypeValue"></param>
    ''' <param name="participantId"></param>
    ''' <param name="roleList"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function SendNotification(ByVal notificationTypeValue As Cdm.DataCore.CaseNotes.Notification.NotificationType, ByVal participantId As Integer, ByVal roleList As Hashtable) As Boolean
        Dim fromEmail As String = System.Web.HttpContext.Current.Application("EmailFromAddress")
        Dim emailServer As String = System.Web.HttpContext.Current.Application("EmailFromServer")
        Dim emailFromDisplayName As String = System.Web.HttpContext.Current.Application("EmailFromDisplayName")
        Return Cdm.DataCore.CaseNotes.Notification.Send(notificationTypeValue, participantId, fromEmail, emailServer, emailFromDisplayName, roleList, ApplicationHomePage())
    End Function

    Public Shared Function ApplicationHomePage() As String
        Dim prefixString As String
        If System.Web.HttpContext.Current.Request.ServerVariables("HTTPS") = "on" Then
            prefixString = "https://"
        Else
            prefixString = "http://"
        End If
        Return prefixString & System.Web.HttpContext.Current.Request.Url.Host & _
               System.Web.HttpContext.Current.Request.ApplicationPath & "/default.aspx"
    End Function

    Public Shared Function FullPath(ByVal pathName As String) As String

        If pathName.StartsWith("~") Then
            

            If pathName.Length > 1 Then
                Return System.Web.HttpContext.Current.Request.ApplicationPath & pathName.Substring(1)
            Else
                Return System.Web.HttpContext.Current.Request.ApplicationPath
            End If
        Else
            Return pathName
        End If
    End Function


    Private Shared Function GetHostName(ByVal isSecure As Boolean) As String
        If isSecure Or System.Web.HttpContext.Current.Request.Url.Port = 80 Then
            Return System.Web.HttpContext.Current.Request.Url.Host
        Else
            Return System.Web.HttpContext.Current.Request.Url.Host() & ":" & System.Web.HttpContext.Current.Request.Url.Port.ToString()
        End If
    End Function

    Public Shared Function AppConnectString() As String
        Return Gentle.Framework.Broker.SessionBroker.Provider.ConnectionString
    End Function


End Class





